package com.hc.redis;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

public class TestRedis {
    public static void main(String[] args) {
        /*连接数据库*/
        Jedis jedis=new Jedis("localhost",6379);    /*不写参数默认是"localhost",6379*/

        /*清空所有数据*/
        jedis.flushAll();
        /*选择数据库*/
        jedis.select(0);

        /*1.String类型的数据操作*/
        jedis.set("name","张三");
        /*获取name的值*/
        System.out.println(jedis.get("name"));

        /*设置name的值为 李四 60秒有效时间*/
        jedis.setex("name",60,"李四");
        /*获取 name 当前剩余的生命时间*/
        System.out.println("name 当前剩余的生命时间"+jedis.ttl("name"));

        /*一次设置多个 keys values*/
        jedis.mset("age","18","pwd","123");

        /*获取多个值*/
        System.out.println(jedis.mget("name", "age", "pwd"));


        /*2.list 类型的操作*/
        jedis.select(1);

        /*list 的存储*/
        jedis.lpush("fruits","apple","pear");
        jedis.rpush("fruits","tomato","banana");

        /*读取*/
        System.out.println(jedis.lrange("fruits", 0, -1));
        System.out.println(jedis.lindex("fruits", 0));

        jedis.lpush("fruits","ww","ww","ww");
        System.out.println(jedis.lrange("fruits", 0, -1));
        /*批量删除*/
        jedis.lrem("fruits",2,"ww");
        System.out.println(jedis.lrange("fruits", 0, -1));

        /*删除list左边第一个数据*/
        System.out.println(jedis.lpop("fruits"));
        System.out.println(jedis.brpop(5,"fruits"));
        System.out.println(jedis.lrange("fruits", 0, -1));
        System.out.println(jedis.brpoplpush("fruits", "temp", 5));

        /*3.set 类型*/
        jedis.select(2);
        /*增*/
        jedis.sadd("mySet","java","php","c++","Python");

        /*随机获取3个*/
        System.out.println(jedis.srandmember("mySet", 3));
        jedis.sadd("mySet","数据结构","go语言");

        /*删除*/
        System.out.println(jedis.srem("mySet", "c++", "python"));
        /*查询*/
        System.out.println(jedis.smembers("mySet"));

        /*获取集合元素个数*/
        System.out.println(jedis.scard("mySet"));

        jedis.sadd("aa","1","2","3","4","5","6");
        jedis.sadd("bb","1","3","5","6");
        jedis.sadd("cc","2","4","6");
        jedis.sadd("dd","99");

        System.out.println("交集"+jedis.sinter("bb", "cc"));
        System.out.println("并集"+jedis.sunion("bb", "cc"));
        System.out.println("并集"+jedis.sinter("dd", "cc"));
        System.out.println("差集"+jedis.sdiff("bb", "cc"));
        /*查询*/
        System.out.println(jedis.smembers("mySet"));

        /*4.sortset */
        jedis.select(3);
        jedis.zadd("grade",85.6,"张三");
        Map<String,Double> gradeMap=new HashMap<>();
        gradeMap.put("李四",89D);
        gradeMap.put("王五",75.3);
        gradeMap.put("赵六",85.5);
        gradeMap.put("田七",95.3);
        jedis.zadd("grade",gradeMap);

        System.out.println(jedis.zrangeByScore("grade", 0D, 100D));

        /*删除*/
        jedis.zrem("grade","田七");

        /*修改grade*/
        jedis.zadd("grade",88D,"赵六");

        Map<String,Double> aaMap=new HashMap<>();
        aaMap.put("李四",89D);
        aaMap.put("王五",75.3);
        aaMap.put("赵六",85.5);
        aaMap.put("田七",95.3);
        jedis.zadd("aa",aaMap);
        System.out.println("交集:"+jedis.zinterstore("aa", "grade"));
        System.out.println("交集:"+jedis.zunionstore("aa", "grade"));

        System.out.println("grade:"+jedis.zrangeByScoreWithScores("grade", 0D, 100D));

        /*5.hash*/
        jedis.select(4);
        Map<String,String> hashMap=new HashMap<>();
        hashMap.put("name","Amy");
        hashMap.put("age","12");
        jedis.hmset("hashSet",hashMap);

        /*删除指定属性*/
        jedis.hdel("hashSet","sex");
        /*查询*/
        System.out.println(jedis.hgetAll("hashSet"));
        /*关闭数据库*/
        jedis.close();
    }
}
